package com.fhx.core.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.LinearLayout
import androidx.annotation.Nullable
import androidx.viewpager.widget.ViewPager


class TabButtonGroup(
    context: Context?, @Nullable attrs: AttributeSet?,
    defStyleAttr: Int
) :
    LinearLayout(context, attrs, defStyleAttr), View.OnClickListener {
    private val mList: MutableList<TabButton>
    private var mViewPager: ViewPager? = null
    private var mCurPosition: Int
    private val mAnimator: ValueAnimator?
    private var mAnimView: View? = null
    private val mRunnable: Runnable
    private var isLogin = false

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, @Nullable attrs: AttributeSet?) : this(context, attrs, 0)

    override fun onFinishInflate() {
        super.onFinishInflate()
        var i = 0
        val count = childCount
        while (i < count) {
            val v: View = getChildAt(i)
            v.tag = i
            v.setOnClickListener(this)
            mList.add(v as TabButton)
            i++
        }
    }



    override fun onClick(v: View) {
        VibrateHelp.vSimple(v.context, 40)
        val tag: Any = v.tag
        if (tag != null) {
            val position = tag as Int
            if (position == mList.size && !isLogin) {
                loginClick?.invoke()
                return
            }

            if (position == mCurPosition) {
                return
            }
            mList[mCurPosition].setChecked(false)
            val tbn = mList[position]
            tbn.setChecked(true)
            mCurPosition = position
            mAnimView = tbn
            mAnimator!!.start()
            postDelayed(mRunnable, 150)
        }
    }

    fun setViewPager(viewPager: ViewPager?) {
        mViewPager = viewPager
    }

    fun resetState() {
        if (!mList.isNullOrEmpty()) {
            for (i in mList.indices) {
                mList[i].setChecked(false)
            }
            mList[0].setChecked(true)
            mCurPosition = 0
        }
    }
    fun setpostion(postion:Int){
        for (i in mList.indices) {
            if (i==postion){
                mList[i].setChecked(true)
                mCurPosition=i
            }else{
                mList[i].setChecked(false)
            }

        }
    }

    fun cancelAnim() {
        mAnimator?.cancel()
    }

    init {
        mList = ArrayList()
        mCurPosition = 0
        mAnimator = ValueAnimator.ofFloat(1.0f, 1.2f, 1f)
        mAnimator.addUpdateListener(AnimatorUpdateListener { animation ->
            val v = animation.animatedValue as Float
            if (mAnimView != null) {
                mAnimView?.scaleX = v
                mAnimView?.scaleY = v
            }
        })
        mAnimator.duration = 350
        mAnimator.interpolator = AccelerateDecelerateInterpolator()
        mAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                mAnimView = null
            }
        })
        mRunnable = Runnable {
            if (mViewPager != null) {
                mViewPager!!.setCurrentItem(mCurPosition, false)
            }
        }
    }

    /*fun setClickCallback(clickCallback: ClickCallback) {
        this.clickCallback = clickCallback
    }

    interface ClickCallback {
        fun result()
    }*/

    var loginClick:(()->Unit)? = null
}